home *** CD-ROM | disk | FTP | other *** search
- #include <stdlib.h>
- #include <math.h>
-
- #include <Carbon/Carbon.h>
-
- void SetRandomBackColor();
- void CenterWindow(Rect* theRect);
- void StartBoard(WindowRef theWindow);
- void HandlePaddle();
- void HandleBall();
- short TheStringWidth(Str255 theString);
-
- double gBallVelX;
- double gBallVelY;
-
- double gBallPosX;
- double gBallPosY;
-
- Rect *gWords;
- short gNumWords=0;
-
-
-
- Rect gPaddleRect;
-
- short gPaddleVel;
-
- void main()
- {
- WindowRef theWindow;
- Rect windowRect={0,0,300,400};
- long t;
- EventRecord theEvent;
-
- gBallPosX = 100;
- gBallPosY = 250;
- gBallVelX = 1.1;
- gBallVelY = -2.4;
-
- CenterWindow(&windowRect);
-
- theWindow = NewWindow(0,&windowRect,"\p",true,1,(WindowRef)-1L,0,0);
- SetQDGlobalsRandomSeed(TickCount());
-
- StartBoard(theWindow);
-
- ForeColor(blackColor);
- BackColor(whiteColor);
-
- while (!Button())
- {
- t = TickCount();
-
- HandlePaddle();
- HandleBall();
-
- while (t==TickCount())
- {
- WaitNextEvent(everyEvent,&theEvent,1,0);
- }
- }
- DisposeWindow(theWindow);
- DisposePtr((Ptr)gWords);
-
- }
-
-
- void SetRandomBackColor()
- {
- switch (abs(Random())%7)
- {
- case 0:
- BackColor(blackColor);
- break;
- case 1:
- BackColor(redColor);
- break;
- case 2:
- BackColor(greenColor);
- break;
- case 3:
- BackColor(blueColor);
- break;
- case 4:
- BackColor(cyanColor);
- break;
- case 5:
- BackColor(magentaColor);
- break;
- case 6:
- BackColor(yellowColor);
- break;
- }
- }
-
- void CenterWindow(Rect* theRect)
- {
- GrafPtr thePort;
- Rect screenRect;
- short leftPos,topPos,rectWidth,rectHeight;
-
- GetPort(&thePort);
- GetPortBounds(thePort,&screenRect);
-
- leftPos = ((screenRect.right - screenRect.left) - (theRect->right -
- theRect->left)) / 2;
- topPos = ((screenRect.bottom - screenRect.top)
- - (theRect->bottom - theRect->top))/2;
-
- rectWidth = theRect->right - theRect->left;
- rectHeight = theRect->bottom - theRect->top;
-
- theRect->top = topPos;
- theRect->bottom = theRect->top + rectHeight;
- theRect->left = leftPos;
- theRect->right = theRect->left + rectWidth;
- }
-
- void StartBoard(WindowRef theWindow)
- {
- Rect windowRect;
- FontInfo fInfo;
- Handle rHandle;
- short numStrings;
- Str255 theString;
- Str255 wordString;
- short cursor,oldCursor;
- short vPos,a;
- Rect blockRect;
- char *stringsUsed;
- short stringToUse;
- short stringsToUseOnLine[8];
- short stringsToUseOnLineIndex;
- short lineWidth;
- float bufferPixels;
-
- rHandle = GetResource('STR#',128);
- if (!rHandle)
- ExitToShell();
-
- BlockMove(*rHandle,&numStrings,sizeof(short));
- ReleaseResource (rHandle);
-
- HideCursor();
-
- stringsUsed = NewPtr(numStrings);
-
- for (a=0;a<numStrings;a++)
- stringsUsed[a]=0;
-
- SetPort(GetWindowPort(theWindow));
- GetPortBounds(GetWindowPort(theWindow),&windowRect);
-
- OffsetRect(&windowRect,-windowRect.left,-windowRect.top);
-
- ForeColor(whiteColor);
- TextSize(10);
- TextFace(bold);
- GetFontInfo(&fInfo);
-
- blockRect.left = 0;
- blockRect.right = 0;
- vPos = 40;
- lineWidth=0;
- stringsToUseOnLineIndex=0;
-
- gWords = (Rect*)NewPtr(sizeof(Rect)*500); //Hack, but it should be enough
-
- while (vPos<12*8+40)
- {
- while(1)
- {
- do {
- stringToUse = abs(Random())%numStrings;
- }while(stringsUsed[stringToUse]);
-
-
- GetIndString(theString,128,stringToUse);
-
- if (TheStringWidth(theString)+lineWidth<400)
- {
- lineWidth+=TheStringWidth(theString);
- stringsToUseOnLine[stringsToUseOnLineIndex] = stringToUse;
- stringsToUseOnLineIndex++;
- stringsUsed[stringToUse]=1;
- }
- else
- break;
- }
- bufferPixels = (400-lineWidth)/(stringsToUseOnLineIndex-1);
-
- for (a=0;a<stringsToUseOnLineIndex;a++)
- {
- SetRandomBackColor();
-
- cursor = 0;
- oldCursor = 1;
-
- GetIndString(theString,128,stringsToUseOnLine[a]);
-
- while (cursor<=theString[0])
- {
- for (cursor=oldCursor;theString[cursor]!=' ' && cursor<=theString[0];cursor++)
- ;
-
- BlockMove(&(theString[oldCursor]),&(wordString[1]),cursor-oldCursor);
- wordString[0] = (char)((char)cursor-(char)oldCursor);
-
- blockRect.top=vPos-fInfo.ascent-fInfo.descent;
- blockRect.bottom = vPos;
- if (blockRect.right)
- blockRect.left = blockRect.right+2;
- blockRect.right = StringWidth(wordString)+blockRect.left;
-
- gWords[gNumWords] = blockRect;
- gNumWords++;
-
- MoveTo(blockRect.left,vPos-fInfo.descent);
- EraseRect(&blockRect);
- DrawString(wordString);
-
- oldCursor = cursor+1;
- }
- blockRect.right+=bufferPixels;
- }
-
- blockRect.right=0;
- blockRect.left=2;
- vPos+=16;
- lineWidth=0;
- stringsToUseOnLineIndex=0;
- }
-
- ShowCursor();
-
- //gNumWords--;
- DisposePtr(stringsUsed);
- }
-
- void HandlePaddle()
- {
- Point mouseLoc;
- static Rect oldPaddleRect;
- Rect removeRect;
-
- //Handle Paddle
- GetMouse(&mouseLoc);
-
- if (mouseLoc.h<0)
- gPaddleRect.left = 0;
- else
- gPaddleRect.left = mouseLoc.h;
-
- gPaddleRect.right = gPaddleRect.left+25;
- if (gPaddleRect.right>400)
- {
- gPaddleRect.right = 400;
- gPaddleRect.left = 370;
- }
- gPaddleRect.top = 300-32;
- gPaddleRect.bottom = gPaddleRect.top+4;
-
- PaintRect(&gPaddleRect);
-
- gPaddleVel = gPaddleRect.right-oldPaddleRect.right;
- if (gPaddleVel>5)
- gPaddleVel=5;
- if (gPaddleVel<-5)
- gPaddleVel=-5;
-
- if (oldPaddleRect.right > gPaddleRect.right)
- {
- removeRect.left = gPaddleRect.right;
- removeRect.right = oldPaddleRect.right;
- }
- else
- {
- removeRect.left = oldPaddleRect.left;
- removeRect.right = gPaddleRect.left;
- }
- removeRect.top = oldPaddleRect.top;
- removeRect.bottom = oldPaddleRect.bottom;
-
- EraseRect(&removeRect);
-
- oldPaddleRect = gPaddleRect;
- }
-
- void HandleBall()
- {
- Rect ballRect;
- Rect oldBallRect;
- short a;
-
- oldBallRect.left = gBallPosX-3;
- oldBallRect.right = gBallPosX+3;
- oldBallRect.top = gBallPosY-3;
- oldBallRect.bottom = gBallPosY+3;
-
- gBallPosX += gBallVelX;
- gBallPosY += gBallVelY;
-
- ballRect.left = gBallPosX-3;
- ballRect.right = gBallPosX+3;
- ballRect.top = gBallPosY-3;
- ballRect.bottom = gBallPosY+3;
-
- EraseRect(&oldBallRect);
- PaintOval(&ballRect);
-
- for (a=0;a<gNumWords;a++)
- {
- if (SectRect(&ballRect,&gWords[a],0))
- {
- //Hit a block. Erase it and bounce the ball
- EraseRect(&gWords[a]);
-
- gWords[a].top=0;
- gWords[a].left=0;
- gWords[a].right=0;
- gWords[a].bottom=0;
-
- gBallVelY =- gBallVelY;
- break;
- }
- }
- if (SectRect(&gPaddleRect,&ballRect,0))
- {
- //Hit the paddle. bounce the ball
- gBallVelX += (gBallPosX-(gPaddleRect.left+12))/12;
- gBallVelY =- gBallVelY;
- }
- if (ballRect.right>=400 || ballRect.left<=0) //Hit side walls
- gBallVelX =- gBallVelX;
-
- if (ballRect.top<=0) //Hit top wall
- gBallVelY =- gBallVelY;
- if (ballRect.bottom>=400)
- {
- gBallVelX = ((float)Random()/(float)20000)+2;
- gBallVelY = ((float)Random()/(float)20000)+2;
- gBallPosX = abs(Random())%300;
- gBallPosY = abs(Random())%100+100;
- }
- }
-
- short TheStringWidth(Str255 theString)
- {
- char a;
- short additionalWidth=0;
-
- for (a=0;a<theString[0];a++)
- if (theString[a+1]==' ')
- additionalWidth+=2;
- return StringWidth(theString)+additionalWidth;
- }